﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.ServiceModel;
using System.Threading;
using AutoMapper;
using Core;
using IglooCoder.Commons.WcfNhibernate;
using Infrastucture;
using WcfService1.DTO;

namespace WcfService1
{
    [ServiceContract(Namespace = "")]
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    [NHibernateContext]
    public class SaldoAwalInventoryServices
    {
        private readonly InventoryRepository InventoryRep;
        private readonly MutasiStokRepository _MutasiRepository;

        public SaldoAwalInventoryServices()
        {
            Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");
            var sesssion = new WcfSessionStorage();
            InventoryRep = new InventoryRepository(sesssion);
            _MutasiRepository = new MutasiStokRepository(sesssion);
        }


        [OperationContract]
        public List<InventoryDTO> GetInventories(int page, out int recordCount, string sortProperty = null,
                                                 string sortType = "ASC")
        {
            var inventoryDtos = new List<InventoryDTO>();
            IList<Inventory> Inventories = new List<Inventory>();

            if (string.IsNullOrEmpty(sortProperty))
            {
                //var criteria = new Dictionary<string,object>();
                //criteria.Add("Qty", "1");
                Inventories = InventoryRep.GetDataPaged(page, 30);
            }
            else
            {
                Inventories = InventoryRep.GetDataPaged(page, 30, sortProperty, sortType);
            }


            foreach (Inventory inventory in Inventories)
            {
                var inventoryDto = new InventoryDTO();
                try
                {
                    inventoryDto = Mapper.Map<Inventory, InventoryDTO>(inventory);
                }
                catch (Exception exception)
                {
                }

                inventoryDtos.Add(inventoryDto);
            }
            recordCount = (int) InventoryRep.RecordCount;
            return inventoryDtos;
        }


        [OperationContract]
        public void SaveInventory(InventoryDTO inventoryDto)
        {
            Inventory inventory = InventoryRep.GetDataOne(x => x.SerialNumber == inventoryDto.SerialNumber);
            Mapper.Map(inventoryDto, inventory);
            InventoryRep.ExecuteTransactionNative(() =>
                                                      {
                                                          //Mutasi Masuk
                                                          var mutasiStok = new MutasiStok();
                                                          mutasiStok.ReferenceNumber = inventory.KodeMasuk;
                                                          mutasiStok.SerialNumber = inventory.SerialNumber;
                                                          mutasiStok.TglMutasi = DateTime.Now;
                                                          mutasiStok.Type = "SaldoAwal";
                                                          mutasiStok.QtyMasuk = inventory.QtyUnit ?? 0;
                                                          mutasiStok.QtySisa = mutasiStok.QtyMasuk;
                                                          mutasiStok.SheetMasuk = inventory.Tinggi ?? 0;
                                                          mutasiStok.SheetSisa = mutasiStok.SheetMasuk;
                                                          mutasiStok.KodeWarehouse =
                                                              inventory.InventoryWarehouses.FirstOrDefault().
                                                                  KodeWarehouse;
                                                          _MutasiRepository.SaveOrUpdate(mutasiStok);
                                                          InventoryRep.SaveOrUpdate(inventory);
                                                      });
        }

        public void DeleteInventory(InventoryDTO inventoryDto)
        {
            var inventory = new Inventory();
            Mapper.Map(inventoryDto, inventory);
            InventoryRep.Delete(inventory);
        }
    }
}